package com.mxx.basic.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * 作者：刘江飞
 * 时间：2020/6/17
 * 邮箱：642371742@qq.com
 * 描述：Activity管理类工具
 */
public class ActivityUtil {
    private static ActivityUtil activityUtil;

    private ActivityUtil() {
    }

    public static ActivityUtil getInstance() {

        if (activityUtil == null) {

            synchronized (ActivityUtil.class) {
                if (activityUtil == null) {
                    activityUtil = new ActivityUtil();
                }
            }
        }
        return activityUtil;
    }


    /**
     * 管理Activity的list集合
     */
    private static List<Activity> activityList = Collections.synchronizedList(new LinkedList<Activity>());

    public List<Activity> getActivityList() {

        return activityList;
    }

    /**
     * 添加一个activity到list里
     */
    public void pushActivity(Activity activity) {

        if (activityList == null) {
            return;
        }

        activityList.add(activity);
    }


    /**
     * 从list里删除一个activity
     */
    public void popActivity(Activity activity) {

        if (activityList == null) {
            return;
        }

        activityList.remove(activity);
    }


    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {

        if (activityList == null || activityList.isEmpty()) {
            return;
        }

        List<Activity> activityList = new ArrayList<>();

        for (Activity activity : activityList) {

            if (activity != null && activity.getClass().equals(cls)) {

                activityList.add(activity);
            }
        }

        activityList.removeAll(activityList);

        for (int i = 0; i < activityList.size(); i++) {

            Activity activity = activityList.get(i);

            if (activity != null) {
                activity.finish();
            }
        }
    }


    /**
     * 结束指定类名的 Activity 集合
     */
    public void finishActivityCollection(@NonNull List<Class<?>> classList) {

        if (activityList == null || activityList.isEmpty()) {
            return;
        }

        List<Activity> needFinishedActivityList = new ArrayList<>();

        for (int i = 0; i < classList.size(); i++) {

            Class<?> aClass = classList.get(i);

            for (Activity activity : activityList) {

                if (activity != null && activity.getClass().equals(aClass)) {

                    if (!needFinishedActivityList.contains(activity)) {
                        needFinishedActivityList.add(activity);
                    }
                }
            }
        }

        activityList.removeAll(needFinishedActivityList);

        for (int j = 0; j < needFinishedActivityList.size(); j++) {

            Activity activity = needFinishedActivityList.get(j);

            if (activity != null) {
                activity.finish();
            }
        }
    }


    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (activityList == null) {
            return;
        }

        List<Activity> needFinishedActivityList = new ArrayList<>();

        for (int i = 0; i < activityList.size(); i++) {

            for (Activity activity : activityList) {

                if (activity != null) {

                    if (!needFinishedActivityList.contains(activity)) {
                        needFinishedActivityList.add(activity);
                    }
                }
            }
        }

        activityList.removeAll(needFinishedActivityList);
        for (int j = 0; j < needFinishedActivityList.size(); j++) {

            Activity activity = needFinishedActivityList.get(j);

            if (activity != null) {
                activity.finish();
            }
        }
    }

    /**
     * 退出登陆后，清空之前所有的activity，进入登陆主界面
     *
     * @param context        当前Activity的上下文对象
     * @param targetActivity 目标Activity
     */
    public void exitToLogin(Context context, Class<?> targetActivity) {
        Intent intent = new Intent(context, targetActivity);
        //将targetActivity至于栈顶
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
        finishAllActivity();
    }
}
